// Package encryption provides file utility functions for encryption operations.
package encryption

import (
	"fmt"
	"os"
)

// EncryptJSONFile encrypts a JSON file using the provided password.
// It reads from inputFile, encrypts the data, and writes the result to outputFile.
func EncryptJSONFile(inputFile, outputFile, password string, encryption JSONEncryption) error {
	// Open input file
	input, err := os.Open(inputFile)
	if err != nil {
		return fmt.Errorf("failed to open input file: %w", err)
	}
	defer input.Close()

	// Create output file
	output, err := os.Create(outputFile)
	if err != nil {
		return fmt.Errorf("failed to create output file: %w", err)
	}
	defer output.Close()

	// Encrypt file
	if err := encryption.Encrypt(input, output, password); err != nil {
		return fmt.Errorf("failed to encrypt file: %w", err)
	}

	return nil
}

// DecryptJSONFile decrypts an encrypted JSON file using the provided password.
// It reads from inputFile, decrypts the data, and writes the result to outputFile.
func DecryptJSONFile(inputFile, outputFile, password string, encryption JSONEncryption) error {
	// Open input file
	input, err := os.Open(inputFile)
	if err != nil {
		return fmt.Errorf("failed to open input file: %w", err)
	}
	defer input.Close()

	// Create output file
	output, err := os.Create(outputFile)
	if err != nil {
		return fmt.Errorf("failed to create output file: %w", err)
	}
	defer output.Close()

	// Decrypt file
	if err := encryption.Decrypt(input, output, password); err != nil {
		return fmt.Errorf("failed to decrypt file: %w", err)
	}

	return nil
}